package com.sun.exception;

import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalException {


    /**
     * 捕获全局的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Map<String,Object> exceptionHandler(Exception e){
        HashMap<String, Object> hashMap = Maps.newHashMap();
        hashMap.put("code","500");
        hashMap.put("message",e.getMessage());
        return hashMap;
    }


    /**
     * 捕获自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    public Map<String,Object> myExceptionHandler(MyException e){
        Map<String,Object> map  = new HashMap<String,Object>();
        map.put("code",e.getCode());
        map.put("message",e.getMessage());
        map.put("method",e.getMethod());
        map.put("descinfo",e.getDescinfo());
        return map;
    }
}
